package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdChannelMapper;
import com.usian.admin.service.AdChannelService;
import com.usian.model.admin.dtos.ChannelDto;
import com.usian.model.admin.pojos.AdChannel;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {


    @Autowired
    AdChannelMapper adChannelMapper;

    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {
        //调用mybatis-plus的api进行列表查询

       //1.参数校验
        if (channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

      //2.分页参数校验
       channelDto.checkParam();

        Page<AdChannel> page = new Page<>(channelDto.getPage(), channelDto.getSize());

        LambdaQueryWrapper<AdChannel> adChannelLambdaQueryWrapper = new LambdaQueryWrapper<>();



        if (StringUtils.isNotBlank(channelDto.getName())){
            adChannelLambdaQueryWrapper.like(AdChannel::getName,channelDto.getName());
        }

        IPage<AdChannel> adChannelIPage = page(page, adChannelLambdaQueryWrapper);

        PageResponseResult pageResponseResult = new PageResponseResult(channelDto.getPage(), channelDto.getSize(), (int) adChannelIPage.getTotal());

        pageResponseResult.setData(adChannelIPage.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1.参数校验
        if (adChannel==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.参数填充
        adChannel.setCreatedTime(new Date());
        //3.进行添加
        boolean saveResult = this.save(adChannel);
        if (!saveResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.SAVE_FAIL);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdChannel adChannel) {

        //1.参数校验
        if (adChannel==null || adChannel.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.填充参数 无
        //3.进行修改
        boolean updateResult = this.updateById(adChannel);
        if (!updateResult){
            return ResponseResult.errorResult(AppHttpCodeEnum.UPDATE_FAIL);
        }
        //4.返回前台
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult delete(Integer id) {
       if (id==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
       }

        AdChannel adChannel = this.getById(id);
       if (adChannel==null){
           return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
       }

       if (adChannel.getStatus()){
           return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效不能删除");
       }
        boolean delResult = this.removeById(id);
       if (!delResult){
           return ResponseResult.errorResult(AppHttpCodeEnum.DELETE_FAIL);
       }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
